package com.nannybear.Controller;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nannybear.R;

import java.util.List;
/**
 * \brief Control the list view on the calendar screen
 * \author Audrey LOISEAU
 * \date 11-05-2015
 */

/********************************* COPYRIGHT *********************************
 * Copyright (c) 2015, Audrey LOISEAU and Benjamin LANCINOT.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *************************************************************************************/

public class ListAdapter extends BaseAdapter{
    /********** DECLARATIONS **********/
    // Class
    private List<RowItem> rowItems;
    private GridCellAdapter gridCellAdapter;

    /************ METHODS ************/
    /**
     * \brief Constructor
     * \param gridCellAdapter The gridCellAdapter class
     * \param items The list of event
     */
    public ListAdapter(GridCellAdapter gridCellAdapter, List<RowItem> items) {
        this.rowItems = items;
        this.gridCellAdapter = gridCellAdapter;
    }

    /**
     * \brief Inflate the list view
     * param position  The position of the item
     * \param convertView   The new view
     * \param parent    The parent view
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        LayoutInflater mInflater = (LayoutInflater) gridCellAdapter.getCalendarController().getViewPuppeteer().getmActivity().getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.event = (TextView) convertView.findViewById(R.id.name_event);
            holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
            holder.imageView = (ImageView) convertView.findViewById(R.id.color_event);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        RowItem rowItem = (RowItem) getItem(position);

        holder.txtDesc.setText(rowItem.getDesc());
        holder.event.setText(rowItem.getTitle());
        holder.imageView.setBackgroundResource(rowItem.getImageId());

        return convertView;
    }

    /**
     * \brief Give the size of the list
     * \return The number of item
     */
    @Override
    public int getCount() {
        return rowItems.size();
    }

    /**
     * \brief Give the position of the item
     * \param position  The position of the item
     * \return position The data at the specific position
     */
    @Override
    public Object getItem(int position) {
        return rowItems.get(position);
    }

    /**
     * \brief Give the row of the item
     * \param position  The position of the item
     * \return position The id of the item at the specific position
     */
    @Override
    public long getItemId(int position) {
        return rowItems.indexOf(getItem(position));
    }
}
